<?php
/**
 * YICMS
 * ============================================================================
 * 版权所有 2014-2017 YICMS，并保留所有权利。
 * 网站地址: http://www.yicms.vip
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Created by PhpStorm.
 * Author: kenuo
 * Date: 2017/11/17
 * Time: 下午4:40
 */

namespace App\Repositories\TotalData;


use App\Models\LogoutLog;
use App\Models\OnlineLog;
use App\Models\Stat;
use App\Models\PayLog;
use App\Models\LoginLog;
use App\Models\Retention;
use App\Models\Ltv;

class StatRepository
{
    //获取所有游戏的 各个渠道的 新增用户
    public function getChannelNewUsers($request)
    {
        $matchCondition = [
            'login_time' => [
                '$gte' => $request['start_time'] . ' 00:00:00',
                '$lte' => $request['end_time'] . ' 23:59:59'
            ],
            'register_time' => [
                '$gte' => $request['start_time'] . ' 00:00:00',
                '$lte' => $request['end_time'] . ' 23:59:59'
            ],
        ];

        if (!empty($request->game_id)) {
            $matchCondition['game_id'] = [
                '$in' => $request->game_id
            ];
        }

        $query = [
            [
                '$match' => $matchCondition
            ],
            ['$group' =>
                [
                    '_id' => [
                        'game_id' => '$game_id',
                        'channel_id' => '$channel_id',
                        'id' => '$uid',
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => [
                        'game_id' => '$_id.game_id',
                        'channel_id' => '$_id.channel_id',
                    ],
                    'new_users' => [
                        '$sum' => 1
                    ]
                ]
            ],
            [
                '$project' => [
                    'key' => [
                        '$concat' => ['$_id.game_id', '-', '$_id.channel_id']
                    ],
                    'new_users' => 1
                ]
            ]
        ];


        $collection = LoginLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }

    //获取所有游戏 各个渠道的 活跃用户
    public function getChannelActiveUsers($request)
    {
        $matchCondition = [
            'login_time' => [
                '$gte' => $request['start_time'] . ' 00:00:00',
                '$lte' => $request['end_time'] . ' 23:59:59'
            ],
        ];

        if (!empty($request->game_id)) {
            $matchCondition['game_id'] = [
                '$in' => $request->game_id
            ];
        }

        $query = [
            [
                '$match' => $matchCondition
            ],
            ['$group' =>
                [
                    '_id' => [
                        'game_id' => '$game_id',
                        'channel_id' => '$channel_id',
                        'id' => '$uid',
                    ],
                    'game_name' => [
                        '$last' => '$game_name'
                    ],
                    'channel_name' => [
                        '$last' => '$channel_name'
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => [
                        'game_id' => '$_id.game_id',
                        'channel_id' => '$_id.channel_id',
                    ],
                    'game_name' => [
                        '$last' => '$game_name'
                    ],
                    'channel_name' => [
                        '$last' => '$channel_name'
                    ],
                    'active_users' => [
                        '$sum' => 1
                    ]
                ]
            ],
            [
                '$project' => [
                    'key' => [
                        '$concat' => ['$_id.game_id', '-', '$_id.channel_id']
                    ],
                    'game_name' => 1,
                    'channel_name' => 1,
                    'active_users' => 1,
                ]
            ],
            [
                '$sort' => [
                    'key' => 1
                ]
            ]
        ];


        $collection = LoginLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }

    //获取所有游戏 各个渠道的 收入
    public function getChannelTotalAmounts($request)
    {
        $matchCondition = [
            'pay_time' => [
                '$gte' => $request['start_time'] . ' 00:00:00',
                '$lte' => $request['end_time'] . ' 23:59:59'
            ],
        ];

        if (!empty($request->game_id)) {
            $matchCondition['game_id'] = [
                '$in' => $request->game_id
            ];
        }

        $query = [
            [
                '$match' => $matchCondition
            ],
            ['$group' =>
                [
                    '_id' => [
                        'game_id' => '$game_id',
                        'channel_id' => '$channel_id',
                    ],
                    'total_amounts' => [
                        '$sum' => [
                            '$toDouble' => '$amounts'
                        ]
                    ],
                ]
            ],
            [
                '$project' => [
                    'key' => [
                        '$concat' => ['$_id.game_id', '-', '$_id.channel_id']
                    ],
                    'total_amounts' => 1,
                ]
            ],
        ];


        $collection = PayLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }

    //获取所有游戏 各个渠道的 活跃付费用户
    public function getChannelPayUsers($request)
    {
        $matchCondition = [
            'pay_time' => [
                '$gte' => $request['start_time'] . ' 00:00:00',
                '$lte' => $request['end_time'] . ' 23:59:59'
            ],
        ];

        if (!empty($request->game_id)) {
            $matchCondition['game_id'] = [
                '$in' => $request->game_id
            ];
        }

        $query = [
            [
                '$match' => $matchCondition
            ],
            ['$group' =>
                [
                    '_id' => [
                        'game_id' => '$game_id',
                        'channel_id' => '$channel_id',
                        'id' => '$uid',
                    ],
                ]
            ],
            ['$group' =>
                [
                    '_id' => [
                        'game_id' => '$_id.game_id',
                        'channel_id' => '$_id.channel_id',
                    ],
                    'pay_users' => [
                        '$sum' => 1
                    ]
                ]
            ],
            [
                '$project' => [
                    'key' => [
                        '$concat' => ['$_id.game_id', '-', '$_id.channel_id']
                    ],
                    'pay_users' => 1,
                ]
            ],
        ];


        $collection = PayLog::raw(function ($collection) use ($query) {
            return $collection->aggregate($query);
        });

        return $collection;
    }
}